#include <QCoreApplication>
#include "Result/Result.h"
#include "Utility/Utility.h"
#include <time.h>
#include <QTextStream>
#include "help.h"

QTextStream cout(stdout,QIODevice::WriteOnly);
QTextStream cin(stdin,  QIODevice::ReadOnly);

QString m_sResult;
//QString m_sScore;
//unsigned int m_nTime;
CResult m_ICTCLAS("D:/QTproject/Qctclas/data/");

int m_nOutputFormat;
int m_nOperateType;

void init()
{
    m_nOutputFormat = -1;
    m_nOperateType = -1;
}

void error()
{
    cout<<"错误：程序初始化异常！"<<endl;
    exit(0);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    init();

    QString m_sSource;
    cin>>m_sSource;
    unsigned int m_nResultNum=m_sSource.length();

    char *sResult,*sSource;
    m_ICTCLAS.m_nOutputFormat=m_nOutputFormat;
    m_ICTCLAS.m_nOperateType=m_nOperateType;
    clock_t start, finish;

    if(m_nOutputFormat!=2)
        sResult=new char [(strlen(help::toChar(m_sSource))+13)*3];
    else
        sResult=new char [(strlen(help::toChar(m_sSource))+13)*50];

    if(m_nResultNum==1)
    {
        start=clock();
        if(!m_ICTCLAS.ParagraphProcessing(help::toChar(m_sSource),sResult))
            error();
        else
            m_sResult=help::toQString(sResult);
        finish=clock();
        //m_nTime=1000*(finish-start)/CLOCKS_PER_SEC;
        //m_sScore=QString::number(m_ICTCLAS.m_dResultPossibility[0]);
        //write log file
    }
    else
    {
        start=clock();
        sSource=new char[strlen(help::toChar(m_sSource))+20];
        strcpy(sSource,SENTENCE_BEGIN);
        strcat(sSource,help::toChar(m_sSource));
        strcat(sSource,SENTENCE_END);
        if(!m_ICTCLAS.Processing(sSource,m_nResultNum))
            error();
        else
        {
            finish=clock();
            //m_nTime=1000*(finish-start)/CLOCKS_PER_SEC;
            m_sResult="";
            QString sTemp;
            for(int i=0;i<m_ICTCLAS.m_nResultCount;i++)
            {
                sTemp="Result "+QString::number(i+1)+"(Score="+QString::number(m_ICTCLAS.m_dResultPossibility[i])+"):";
                m_sResult+=sTemp;
                m_ICTCLAS.Output(m_ICTCLAS.m_pResult[i],sResult,true);
                m_sResult+=help::toQString(sResult);
                m_sResult+="\r\n";
            }
        }
        delete [] sSource;
    }
    delete [] sResult;
    cout<<m_sSource+"\r\n"+m_sResult+"\r\n";

    return a.exec();
}
